package Dialog;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import com.example.zelonghu.testgps.R;


/**
 * @name SelectPicPopupWindow
 * @class name：SelectPicPopupWindow
 * @class describe
 * @author zelonghu
 * @time 2019/6/27 8:26 PM
 * @change
 * @chang time
 * @class 底部弹窗Window
 */

public class CatSelectPicPopupWindow extends PopupWindow {


    private Button btn_rigister, btn_describe, btn_cancel;
    private View mMenuView;

    /**
     * @author zelonghu
     * @time 2019/6/27  8:26 PM
     * @describe 构建底部弹窗window
     * @param context Activity
     * @param itemsOnClick 两个按钮的Listener
     */
    public CatSelectPicPopupWindow(Activity context, OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.cat_popup_dialog_layout, null);
        btn_rigister = (Button) mMenuView.findViewById(R.id.btn_rigister);
        btn_describe = (Button) mMenuView.findViewById(R.id.btn_detial);
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        //取消按钮
        btn_cancel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        //设置按钮监听
        btn_rigister.setOnClickListener(itemsOnClick);
        btn_describe.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);

        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()== MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

}